﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.IO;
using System.Windows.Media.Imaging;
using System.Globalization;
using System.Windows;

namespace Medecine.Interface.Converters
{
	/// <summary>
	/// Converter from <c>byte[]</c> to <c>BitmapImage</c>.
	/// </summary>
	public class BytesToImageConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value == null)
				return DependencyProperty.UnsetValue;
			byte[] byteArray = (byte[])value;
			BitmapImage image = new BitmapImage();
			using (MemoryStream stream = new MemoryStream(byteArray))
			{
				image.BeginInit();
				image.CacheOption = BitmapCacheOption.OnLoad;
				image.StreamSource = stream;
				image.EndInit();
			}
			return image; 
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
